<?php

namespace app\api\controller\zi;

use app\api\service\ZiS;
use app\common\controller\Api;
use think\Validate;
use Throwable;
use app\common\model\Share;

class Info2 extends Api
{
    protected $noNeedLogin = 'index';
    protected $noNeedRight = 'index';

    public function index()
    {
        $validate = new Validate();
        if (!$validate->check(input(), [
            'xing' => 'require',
            'ming' => 'require',
        ])) {
            $this->error($validate->getError());
        }
        $xing     = input('xing');
        $ming     = input('ming');
        $xing_arr = ZiS::split($xing);
        $ming_arr = ZiS::split($ming);
        if (count($xing_arr) > 2 || count($ming_arr) > 2) {
            $this->error('姓或名最多只能两个字');
        }
        $user = $this->auth->getUser();
        try {
            $info = ZiS::translate($xing_arr, $ming_arr, $user && ($user['grade'] == 1 || $user['grade'] == 2 ) && $user['number'] != 0);
        } catch (Throwable $e) {
            $this->error($e->getMessage());
        }
        // try {
        //     $share = new Share();
        //     $share->reduce_number($user->id, $xing, $ming, $info);
        // } catch (Throwable $e) {
        //     $this->error($e->getMessage());
        // }
        $this->success('', [
            'grade'  => $user['grade'],
            'number' => $user['number'],
            'price'  => config('site.ddgm'),
            'info'   => $info,
        ]);
    }
}
